//
//  WifiUtils.m
//  ForYourBeauty
//
//  Created by Kelly xu on 2019/5/27.
//  Copyright © 2019 changrenTech. All rights reserved.
//

#import "WifiUtils.h"
#import <SystemConfiguration/CaptiveNetwork.h>
//#import "DeviceInfHelper.h"
@implementation WifiUtils
/**
 *  获取SSID信息
 *
 *  @return id
 */
+(NSString *)fetchSSIDInfo {
    NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
    id info = nil;
    for (NSString *ifnam in ifs) {
        info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
        
        if (info && [info count]) {
            break;
        }
    }
    return info;
}
+ (NSString *)getWifiName{
    NSString *currentSSID = @"";
    CFArrayRef myArray = CNCopySupportedInterfaces();
    if (myArray != nil){
        NSDictionary* myDict = (__bridge NSDictionary *) CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
        if (myDict!=nil){
            currentSSID=[myDict valueForKey:@"SSID"];
        } else {
            currentSSID=@"<<NONE>>";
        }
    } else {
        currentSSID=@"<<NONE>>";
    }
    
    NSArray *ifs = (__bridge id)CNCopySupportedInterfaces();
    NSLog(@"%s: Supported interfaces: %@", __func__, ifs);
    id info = nil;
    for (NSString *ifnam in ifs) {
        info = (__bridge id)CNCopyCurrentNetworkInfo((CFStringRef)CFBridgingRetain(ifnam));
        if (info && [info count]) {
            break;
        }
    }
    
    NSLog(@"wifi info %@",info);
    if ([[info allKeys] count]) {
        return info[@"SSID"];
    }else{
        return @"";
    }
}
+(void)doLoginWork{
//    NSString *userName=[[NSUserDefaults standardUserDefaults] objectForKey:APP_USER_NAME];
//    NSString *userPwd=[[NSUserDefaults standardUserDefaults] objectForKey:APP_USER_PWD];
//
//    [SVProgressHUD showWithStatus:NSLocalizedString(@"Waitting",@"Waitting")];
//    // 隐藏等待的图标
//    [SVProgressHUD dismiss];
//
//    // 用户名和密码不要为空，则说明，已经注册过，则直接登录。
//    if (([userName isEqualToString:@""] && userName != nil) && [userPwd isEqualToString:@""] && userPwd != nil) {
//        [WifiUtils startLogin:userName andPwd:userPwd];
//    }
//    // 开始注册
//    else {
//        NSString *identifier = [[UIDevice currentDevice].identifierForVendor UUIDString];
//        // 取出设备号的后8位，并加上hlk.com，代表IOS端设备的账号（qq.com代表Android端的）
//        NSString *strUser = [NSString stringWithFormat:@"%@@hlk.com",[identifier substringFromIndex:identifier.length - 8]];
//        // 开始注册
//        [[DeviceInfHelper shareInstance] gotoRegisterNewUser:strUser withPwd:@"888888" withPID:kProductID endBlock:^(id result, NSError *err) {
//            if (err) {
//                [SVProgressHUD dismiss];
//                NSString *error = [NSString stringWithFormat:@"%@",err];
//                Toast(error);
//                NSLog(@"注册返回的结果：%@", err);
//            }else{
//                NSLog(@"注册返回的结果：%@", result);
//
//                if( [result[@"status"]intValue]== 1){
//                    //请求成功
//                    // [self.view makeToast:@"注册成功！"];
//                    [WifiUtils startLogin:strUser andPwd:@"888888"];
//
//                }
//                else {
//                    NSString *strMsg = result[@"msg"];
//                    if ([strMsg isEqualToString:@"用户已存在"]) {
//                        // 继续登录
//                        [WifiUtils startLogin:strUser andPwd:@"888888"];
//                    }
//                    else {
//                        Toast(strMsg);
//                        [SVProgressHUD dismiss];
//                    }
//
//                }
//            }
//        }];
//    }
//
}

/** 开始登录 */
+(void)startLogin:(NSString *)strUser andPwd:(NSString *)strPwd{
    
    //    将账号密码存本地，下次自动登录
    //    [[NSUserDefaults standardUserDefaults] setObject:strUser forKey:APP_USER_NAME];
    //    [[NSUserDefaults standardUserDefaults] setObject:@"888888" forKey:APP_USER_PWD];
    // 开始登陆
//    [[DeviceInfHelper shareInstance] gotoLoginUser:strUser withPwd:strPwd endBlock:^(id result, NSError *err) {
//        if (err) {
//            [SVProgressHUD dismiss];
//            NSString *error = [NSString stringWithFormat:@"%@",err];
//            Toast(error);
//            NSLog(@"登录返回的结果：%@", err);
//        }else{
//            [SVProgressHUD dismiss];
//            NSLog(@"登录返回的结果：%@，%@", result,strUser);
//            if( [result[@"status"]intValue]== 1){
//                // 将账号密码存本地，下次自动登录
//                [[NSUserDefaults standardUserDefaults] setObject:strUser forKey:APP_USER_NAME];
//                [[NSUserDefaults standardUserDefaults] setObject:strPwd forKey:APP_USER_PWD];
//                // 开始建立连接
//                [[DeviceInfHelper shareInstance]configLink:self];
//            }
//            else {
//                NSString *strMsg = result[@"msg"];
//                //                [self.view makeToast:strMsg];
//            }
//        }
//    }];
}

@end
